﻿using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Windows;

namespace JYYQ.BMS
{
    /// <summary>
    /// DataCurve.xaml 的交互逻辑
    /// </summary>
    public partial class DataCurve : Window
    {
        public DataCurve()
        {
            InitializeComponent();
        }

        public DataCurve(IList<ChargeData> Data)
        {
            InitializeComponent();
            this.DataContext = this;
            this.List = Data;
            this.CurveModel = CreatModel();
        }

        public PlotModel CurveModel { set; get; }

        public IList<ChargeData> List { set; get; }

        private Series CreateSeries(IList<ChargeData> data, int index)
        {
            var ls = new LineSeries
            {
                Title = "电压充电曲线",
                FontSize = 12,
                FontWeight = OxyPlot.FontWeights.Bold,

                ToolTip = "充电电压值"
            };

            var ls1 = new LineSeries
            {
                Title = "电流充电曲线",
                FontSize = 12,
                FontWeight = OxyPlot.FontWeights.Bold,
                ToolTip = "充电电流值"
            };

            foreach (var item in data)
            {
                ls.Points.Add(new DataPoint(Convert.ToDouble(item.TimeSpan), item.Voltage));
                ls1.Points.Add(new DataPoint(Convert.ToDouble(item.TimeSpan), item.Current));
            }

            return index > 0 ? ls : ls1;
        }

        private PlotModel CreatModel()
        {
            var plot = new PlotModel
            {
                Title = "充电曲线"
            };

            plot.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Left,
                TickStyle = TickStyle.Inside
            });
            plot.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Bottom,
                Title = "充电时长",
                Unit = "分钟",
                TickStyle = TickStyle.Inside
            });

            plot.Series.Add(CreateSeries(this.List, 0));
            plot.Series.Add(CreateSeries(this.List, 1));
            return plot;
        }
    }
}